package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnStar;
    private Button btnStarIntent;
    public Button btnFront;
    private Button btnReverse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStar = findViewById(R.id.btn_star);
        btnStarIntent = findViewById(R.id.btn_star_intent);
        btnFront = findViewById(R.id.btn_star_front);
        btnReverse = findViewById(R.id.btn_reverse);
//Intent服务响应
        btnStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });

        btnStarIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyFrontService.class);
                intent.putExtra("URL", "http://baidu.com");
                startService(intent);
            }
        });

        //顶部进程图标显示
        btnFront.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyFrontService.class);
                startService(intent);
            }
        });


        //实现字母的逆序
        btnReverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = "i am a boy";
                reverse(str);

            }
        });
    }

    private void reverse(String str) {
        String[] words = str.split(" ");
        StringBuffer resultBuffer = new StringBuffer();
        for(String word : words){
            StringBuffer wordBuffer = new StringBuffer(word);
            resultBuffer.append(wordBuffer.reverse()).append(" ");

        }
        Log.e("MainActivity","resultBuffer = " + resultBuffer.toString());


    }
}
